package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */
import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Category;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.Job;
import org.vectrics.recruiter.job.JobTerm;

public class TermsComboTag extends TagSupport {

	public static final long serialVersionUID = 623461242421145L;
    
	static private Category log = Category.getInstance(TermsComboTag.class);
    private String styleClass = null;
    private String property = null;
    private String jobId = null;
   
    public TermsComboTag() {
    	super();
    }


	public int doStartTag() throws JspException  {
		StringBuffer html = new StringBuffer();
		JobTerm termSelect = null;
		
		if (property == null) {
			throw new JspException("Property attribute not specified.");
		}
		log.debug("doStartTag() - property = " + property);
		Job job = null;

		if (jobId != null) {
			job = RecruitServiceLocator.getJobProvider().findJob(new Long(jobId));
		}

		if (job == null) {
			log.debug("Could not find job with id");
		}
		else {
			termSelect = job.getJobTerm();
		}

		
		//HttpSession session = super.pageContext.getSession();
		if (styleClass == null) {
			html.append("<select name=\"" + property + "\" style=\"width:250;font-family:verdana,arial,sans serif;font-size=" +
					"11px\">");
		}
		else {
			html.append("<select name=\"" + property + "\" class=\"" + styleClass + "\">");
		}

		String selectStr = "";

		List<JobTerm> terms = RecruitServiceLocator.getJobProvider().findAllJobTerm();
		for (JobTerm term : terms){

			if ((termSelect!=null)&&(term.getId().equals(termSelect.getId()))) {
				log.debug("Job has contract term");
				selectStr = "SELECTED";
			}		
			else {
				selectStr = "";
			}
			
			html.append("<option value=\"" + term.getCode() + "\"" + selectStr + ">" 
					+ term.getDescription() + "</option>");
		
		}
		html.append("</select>");
		
		JspWriter writer = pageContext.getOut();
		try {
			writer.print(html.toString());
		}
		catch (IOException ex) {
			throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
		}
		return(EVAL_PAGE);
	}


	/**
	 * @return
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 */
	public void setProperty(String property) {
		this.property = property;
	}

	/**
	 * @return
	 */
	public String getJobId() {
		return jobId;
	}

	/**
	 * @param jobId
	 */
	public void setJobId(String jobId) {
		this.jobId = jobId;
	}

}
